package com.example.graduate.Controller;

import com.example.graduate.Bean.*;
import com.example.graduate.Service.CardsCollectService;
import com.example.graduate.Service.ClientService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class LearnController {
    @Autowired
    private CardsCollectService cardsCollectService;
    @Autowired
    private ClientService clientService;

    //通过卡片集合id查找这个卡片集合的信息
    @RequestMapping("/getCards/{userId}")
    @ResponseBody
    public CardsCollect hello(@PathVariable(name = "userId")  String userId){

        Client client=clientService.selectByPrimaryKey(Integer.valueOf(userId));

        int learnIndex =client.getLearncardsid();

        CardsCollect cardsCollect=cardsCollectService.selectCards(learnIndex);

        cardsCollect.setLearnIndex(client.getLearnindex());


        return cardsCollect;
    }


    @PostMapping("/updateStudy/{id}/{nowStudy}")
    @ResponseBody
    public void updateStudy(@PathVariable(name = "id") String id,@PathVariable(name="nowStudy") String nowStudy){
        Client client=new Client();
        client.setId(Integer.valueOf(id));
        client.setNowdaynumber(Integer.valueOf(nowStudy));
        clientService.updateByPrimaryKeySelective(client);
    }

    @GetMapping("/sign/{id}")
    @ResponseBody
    public void updateStudy(@PathVariable(name = "id") String id){
        UserLogin userLogin=new UserLogin();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        userLogin.setPid(Integer.valueOf(id));
        userLogin.setLoginTime(date);
        clientService.sign(userLogin);
    }

    @RequestMapping("/getPaperQuestion/{paperId}")
    @ResponseBody
    public List<Paperquestion> getPaperQuestion(@PathVariable(name = "paperId")  String paperId){
        List<Paperquestion> list =clientService.selectPaperquestionByPaperId(paperId);
        for(int i=0;i<list.size();i++){
            list.get(i).setOption();
        }
        return  list;
    }

    //
    @RequestMapping("/getPaper")
    @ResponseBody
    public List<Paper> getPaper(){

       return clientService.selectPaper();

    }

    @RequestMapping("/getWrongPaper/{userId}")
    @ResponseBody
    public List<Paper> getWrongPaper(@PathVariable(name = "userId")  Integer userId){

        List<Wrongqueston> list=clientService.selectByPrimaryKeys(userId);
        List<Paper> result= new ArrayList<Paper>();
        for(int i=0;i<list.size();i++){
            Paper item = new Paper();
            item=clientService.selectById(list.get(i).getPaperid());
            result.add(item);
        }

        return result;

    }


    @PostMapping("/addWrongRecord")
    @ResponseBody
    public  void addWrongRecord(@RequestBody Wrongqueston wrongqueston){
        System.out.println(wrongqueston.getUserid());
        List<Integer> wrongList=wrongqueston.getWrongList();
        for(int i=0;i<wrongList.size();i++){
            Wrongqueston item= new Wrongqueston();
            item.setPaperid(wrongqueston.getPaperid());
            item.setUserid(wrongqueston.getUserid());
            item.setPaperquestonid(wrongList.get(i));
            clientService.insert(item);
        }

    }

    @RequestMapping("/getWrongPaperQuetion/{userId}/{paperId}")
    @ResponseBody
    public List<Paperquestion> getWrongPaper(@PathVariable(name = "userId")  Integer userId,@PathVariable(name = "paperId")  Integer PaperId){

        List<Paperquestion> result= new ArrayList<Paperquestion>();
        List<Wrongqueston> list=clientService.selectById(userId, PaperId);
        for(int i=0;i<list.size();i++){
            Paperquestion item =clientService.selectPaperquestionById(list.get(i).getPaperquestonid());
            item.setOption();
            result.add(item);
        }
        return result;

    }

    @RequestMapping("/getHistoryLine")
    @ResponseBody
    public List<Historyline> getHistoryLine( ){


        return clientService.selectAllHistoryLine();

    }

    @RequestMapping("/getHistoryLine/{lineId}")
    @ResponseBody
    public List<Historycontent> getHistoryContent(@PathVariable(name = "lineId")  Integer lindId ){
        return clientService.selectHistoryContentByPrimaryKey(lindId);

    }

    @RequestMapping("/getKnowledge")
    @ResponseBody
    public List<Knowledge> getKnowledge( ){


        return clientService.selectByPrimaryKey();

    }

    @RequestMapping("/getKnowledge2/{id}")
    @ResponseBody
    public List<Knowledge2> getKnowledge2(@PathVariable(name = "id")  Integer lindId){


        return clientService.select5ByPrimaryKey(lindId);

    }

    @RequestMapping("/getKnowledgeArticle/{id}")
    @ResponseBody
    public Knowledgearticle getKnowledgeArticle(@PathVariable(name = "id")  Integer lindId){


        return clientService.selectkonwByPrimaryKey(lindId);

    }

    @RequestMapping("/getMap")
    @ResponseBody
    public List<Mapcontent> getMap(){

        List<Mapcontent> result=clientService.selectMapByPrimaryKey();
        for(int i=0;i<result.size();i++){
            result.get(i).makeCallout();
        }

        return result;

    }

    @RequestMapping("/getMapInside/{id}")
    @ResponseBody
    public Mapinside getMapinside(@PathVariable(name = "id")  Integer id){


        return clientService.selectMapInsideByPrimaryKey(id);

    }




}
